<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/1/9
 * Time: 下午1:04
 */

namespace EasySwoole;

use App\Lib\Redis\Redis;
use \EasySwoole\Core\AbstractInterface\EventInterface;
use EasySwoole\Core\Component\Di;
use EasySwoole\Core\Swoole\Process\ProcessManager;
use \EasySwoole\Core\Swoole\ServerManager;
use \EasySwoole\Core\Swoole\EventRegister;
use \EasySwoole\Core\Http\Request;
use \EasySwoole\Core\Http\Response;
use EasySwoole\Core\Utility\File;
use App\Lib\Process\Consumer1;

Class EasySwooleEvent implements EventInterface {

    public static function frameInitialize(): void
    {
        // TODO: Implement frameInitialize() method.
        date_default_timezone_set('Asia/Shanghai');

        //初始化数据库, 初始化完成后, 既可以在全局任意位置使用Db类和模型类进行操作, 报错。。。
        // 获得数据库配置
        //$dbConf = Config::getInstance()->getConf('database');
        //var_dump($dbConf);
        // 全局初始化
        //D//b::setConfig($dbConf);

        //加载所有的配置文件
        self::loadConf(EASYSWOOLE_ROOT.'/Config');

    }

    public static function loadConf($confPath){
        $conf = Config::getInstance();
        $files = File::scanDir($confPath);
        foreach ($files as $file){
            $data = require_once $file;
            $conf->setConf(strtolower(basename($file, '.php')), (array)$data);
        }
    }

    public static function mainServerCreate(ServerManager $server,EventRegister $register): void
    {
        // TODO: Implement mainServerCreate() method.

//        Di::getInstance()->set('MYSQL',\MysqliDb::class,Array (
//                'host' => '139.9.119.67',
//                'username' => 'root',
//                'password' => '123456@',
//                'db'=> 'test',
//                'port' => 13306,
//                'charset' => 'utf8')
//        );

        //把redis注入Di容器
        Di::getInstance()->set('REDIS', Redis::getInstance());

        //创建处理redis任务队列的进程
//        $process_num = 3;
//        for($i=0; $i<$process_num; $i++){
//            ProcessManager::getInstance()->addProcess('consumer1_test_'.$i, Consumer1::class);
//        }

    }

    public static function onRequest(Request $request,Response $response): void
    {
        // TODO: Implement onRequest() method.
    }

    public static function afterAction(Request $request,Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}